package com.yylany.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.yylany.bo.GoodsBOBase;
import com.yylany.vo.GoodsInfo;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Description: 用于校验用户传过来的信息
 *
 * @author <a href="mailto:17777502615@163.com">yaoyonglong</a>
 * @date 2018/12/18
 * @since 1.0.0
 */
public class Verification {

    /**
     * 校验手机的正则表达式
     */
    static final String REGEX = "^((13[0-9])|(14[5,7,9])|(15([0-3]|[5-9]))|(166)|(17[0,1,3,5,6,7,8])|(18[0-9])|(19[8|9]))\\d{8}$";
    /**
     * 校验手机的正则
     */
    static final Pattern COMPILE = Pattern.compile(REGEX);

    /**
     * 校验手机号码
     *
     * @param mobile 输入的手机号码
     * @return 如果校验成功，则返回true,否则 false
     */
    public static Boolean verifyMobile(String mobile) {

        Matcher matcher = COMPILE.matcher(mobile);
        if (mobile != null && matcher.matches()) {
            return true;
        }
        return false;
    }

    public static Boolean verifyString(String str) {

        if (StringUtils.isNotEmpty(str)) {

        }


        return null;
    }

    /**
     * 用于解析 从前台传过来的数据    [[1,2],[2,7]] 转为 1:2|2:7
     *
     * @param str [[1,2],[2,7]]
     * @return 1:2|2:7
     */
    public static String jsonFormatSQL(String str) {

        JSONArray gson = JSON.parseArray(str);
        Iterator<Object> iterator = gson.iterator();

        StringBuilder sb = new StringBuilder();

        while (iterator.hasNext()) {
            List<Integer> next = (List<Integer>) iterator.next();
            String st = next.get(0) + ":" + next.get(1);
            if (iterator.hasNext()) {
                sb.append(st + "|");
            } else {
                sb.append(st);
            }
        }
        return sb.toString();
    }

    /**
     * 进行对象拷贝，
     *
     * @param goods GoodsInfo
     * @param sku   TbSku
     */
    public static void TbSkuTOGoodsInfo(GoodsInfo goods, GoodsBOBase sku) {



        HashMap<String, Object> map = new HashMap<>();
        map.put("id_list", sku.getSpecListCode());
        map.put("id_txt", sku.getSpecList());
        goods.setSpecInfo(map);
    }



}
